﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic;
using LotteryVote.Logic.Web;
using LotteryVote.Utility;
using LotteryVote.Logic.Application;
using LotteryVote.Entity;

namespace LotteryVote
{
    public partial class LotteryResult : AuthorizePage
    {
        public string SearchDate { get; private set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            Execute(() =>
            {
                if (!IsPostBack)
                    BindLotterResules(DateTime.Today);
            });
        }

        void BindLotterResules(DateTime date)
        {
            LotteryResultFacade ltrFacade = new LotteryResultFacade();
            rpt_LotteryResults.DataSource = GetFillNotYetOpen(date, ltrFacade.GetLotteryResultByDate(date));
            rpt_LotteryResults.DataBind();
        }

        private IEnumerable<LotteryVote.Entity.LotteryResult> GetFillNotYetOpen(DateTime date, IEnumerable<LotteryVote.Entity.LotteryResult> lotteryResult)
        {
            if (date != DateTime.Today) return lotteryResult;
            NumLenFacade numFacade = new NumLenFacade();
            List<LotteryVote.Entity.LotteryResult> lotteryResultList = new List<Entity.LotteryResult>(lotteryResult);
            foreach (var company in TodayLotteryCompany.Instance.GetTodayCompany())
            {
                if (!lotteryResult.Contains(it => it.Company.CompanyId == company.CompanyId))
                {
                    var companyNumLen = numFacade.GetNumLengthByCompanyId(company.CompanyId).Sum(it => it.Count);
                    var resords = new List<LotteryRecord>(companyNumLen);
                    for (int i = 0; i < companyNumLen; i++)
                        resords.Add(LotteryRecord.Empty);
                    var result = new LotteryVote.Entity.LotteryResult(company, resords, DateTime.Today);
                    lotteryResultList.Add(result);
                }
            }
            return lotteryResultList;
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            Execute(() =>
            {
                DateTime date;
                if (!DateTime.TryParse(txt_date.Text, out date))
                {
                    ShowMessage("Error", GetGlobalResourceObject("Resource", "Date") + "" + GetGlobalResourceObject("Resource", "Error"));
                    return;
                }
                BindLotterResules(date);
            });
        }

        protected void btn_today_Click(object sender, EventArgs e)
        {
            Execute(() =>
            {
                BindLotterResules(DateTime.Today);
            });
        }

        protected void btn_yesterday_Click(object sender, EventArgs e)
        {
            Execute(() =>
            {
                var date = DateTime.Today.AddDays(-1);
                BindLotterResules(date);
            });
        }
    }
}